package cc.ycws.core.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 访问ice接口类(获取相应的小区楼栋信息)
 *
 * @author wwl
 */
public class ListString {

    public static List<String> list(String GET_URL) {
        List<String> list = new ArrayList<>();
        try {
            URL url = new URL(GET_URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            connection.disconnect();
            new JSONObject();
            JSONObject json = JSONObject.parseObject(sb.toString());
            System.out.println("==json==" + json);
            JSONObject content = json.getJSONObject("content");
            JSONObject housetype = content.getJSONObject("housetype");
            Set keys = housetype.keySet();
            String value = "";
            for (Object key : keys) {
                value = housetype.getString((String) key);
                System.out.println("key:" + key.toString() + "\t value:" + value);
                list.add(value);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<String> list1(String GET_URL) {
        List<String> list = new ArrayList<>();
        try {
            URL url = new URL(GET_URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            connection.disconnect();
            new JSONObject();
            JSONObject json = JSONObject.parseObject(sb.toString());
            System.out.println("==json==" + json);
            String content = json.getString("content");
            JSONArray contentjson = JSONArray.parseArray(content);
            for (int i = 0; i < contentjson.size(); i++) {
                JSONObject job = contentjson.getJSONObject(i);    // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                String value = (String) job.get("buildingName");    // 得到 每个对象中的属性值
                System.out.println(value);
                list.add(value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}

